package com.quanyan.club.web;

import com.quanyan.api.APIResponse;
import com.quanyan.club.service.ClubCallBackService;
import com.quanyan.mq.client.response.MqResponse;
import com.quanyan.mq.enums.MqSendStatus;
import com.quanyan.orderpay.response.mq.OrderNotifyMsg;
import com.quanyan.orderpay.response.mq.RefundNotifyMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.thymeleaf.util.MapUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * Created by zhang on 16/8/5.
 */
@RestController
@RequestMapping(value = "/api/club/callBack/", method = RequestMethod.POST)
public class ClubCallBackController extends BaseController {

    @Autowired
    private ClubCallBackService clubCallBackService;

    /**
     * 俱乐部充值成功回调接口
     * @param orderNotifyMsg
     * @return
     */
    @RequestMapping(value = "rechargePaySuccess")
    public APIResponse rechargePaySuccess(@RequestBody OrderNotifyMsg orderNotifyMsg){

        APIResponse response = this.clubCallBackService.rechargePaySuccess(orderNotifyMsg);

        return response;
    }

    /**
     * 俱乐部充值关闭会调接口
     * @param orderNotifyMsg
     * @return
     */
    @RequestMapping(value = "rechargePayClose")
    public APIResponse rechargePayClose(@RequestBody OrderNotifyMsg orderNotifyMsg){

        APIResponse response = this.clubCallBackService.rechargePayClose(orderNotifyMsg);

        return response;
    }

    /**
     * 俱乐部充值异常退款回调接口
     * @param refundNotifyMsg
     * @return
     */
    @RequestMapping(value = "rechargeApplyRefundCallBack")
    public APIResponse rechargeApplyRefundCallBack(@RequestBody RefundNotifyMsg refundNotifyMsg){

        APIResponse response = this.clubCallBackService.rechargeApplyRefundCallBack(refundNotifyMsg);

        return response;
    }

    /**
     * 俱乐部充值异常退款成功会调接口
     * @param refundNotifyMsg
     * @return
     */
    @RequestMapping(value = "rechargeRefundCallBack")
    public APIResponse rechargeRefundCallBack(@RequestBody RefundNotifyMsg refundNotifyMsg){

        APIResponse response = this.clubCallBackService.rechargeRefundCallBack(refundNotifyMsg);

        return response;
    }

    /**
     * 俱乐部分享回调
     * @param map
     * @return
     */
    @RequestMapping(value = "share")
    public APIResponse share(@RequestBody Map map,HttpServletRequest request){
        Integer uid = 0;
        if (!MapUtils.containsKey(map, "clubId")) {
            return APIResponse.returnFail("请传入必要参数");
        }
        Integer clubId = (Integer) map.get("clubId");
        if (!MapUtils.containsKey(map, "clubType")) {
            return APIResponse.returnFail("请传入必要参数");
        }
        Integer clubType = (Integer) map.get("clubType");
        MqResponse mqResponse = this.clubCallBackService.share(uid,clubId,clubType);
        if (mqResponse.getMqSendStatus().equals(MqSendStatus.SEND_OK)) {
            return APIResponse.returnSuccess();
        }else{
            return APIResponse.returnFail("分享回调失败");
        }
    }


}
